<?php
namespace app\admin\controller;
use app\service\adminService;
use think\Controller;
use think\Db;
use think\captcha\Captcha;
use think\facade\Session;
class Login extends Controller
{
	public function login()
	{       
        $request=request();
        if($request->isGet()){
            return $this->fetch('login');
        }else{
            $data = $request->post();
            $validate = $this->validate($data,[
                            'name|名称'	=>  'require|length:2,15',
                            'password|密码'	=>  'require|length:6,12|alphaNum'
                            
                    ]);

            if($validate !== true){
               $this->error($validate);
            }
             $config = array(

                'reset' => false // 验证成功后是否重置，—————这里才是有效的。
    
            );

            $captcha = new Captcha($config);
            
            /*if(!$captcha->check(trim($data['captcha']))){
    			$this->error('验证码错误');
    		}*/
            $result = (new adminService())->login($data);
    		if(!empty($result)){
    			session('id',$result['id']);
    			session('name',$result['name']);
    			$this->redirect(url('admin/index/index'));
    		}else{
    			$this->error('登陆失败！');
    		}
        }  
    }
	public function Logout(){
		session(null);
		$this->redirect(url('admin/login/login'));
	}
}